const express = require('express');
const request = require('request');
const session = require('express-session')
const router = express.Router();

/* GET order page. */
router.get('/', function(req, res, next) {
  if(req.session.logged) {
    res.render('order', {
      uid: req.session.uid,
      name: req.session.name,
      studentID: req.session.studentID
    });
  }
  else {
    res.redirect('/signin')
  }
  
});

router.post('/', function(req, res, next) {
  console.log(req.body)
  // const url = 'http://localhost:5000/order'
  const url = 'http://order:5000/order'
  request({
      url: url,
      method: "POST",
      json: true,
      body: JSON.stringify(req.body)
  }, function(error, response, body){
      if(!error && response.statusCode == 200){
          if(body.status == 1){
              res.render('remind', {message: body.message})
          }
          else{
              res.send(body.message)
          }
      }
      else
      {
          res.send('Error, try again')
      }

  });
});

module.exports = router;